package com.nativetalk.service.found.insert;

import com.nativetalk.base.RetInfo;
import com.nativetalk.common.Constants;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.CommonUtil;
import com.pingplusplus.Pingpp;
import com.pingplusplus.model.Charge;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by lizhun on 16/1/5.
 * Modified by wangzheng on 17/03/18
 */
@Service
public class FoundChangeInsertServiceImpl implements FoundChangeInsertService {
    private static Logger logger = Logger.getLogger(FoundChangeInsertServiceImpl.class);

    @Override
    public RetInfo insertFoundChange(Integer memb_id, Float mfchg_change_amount, String ip, String channel) {
        String logInfo = this.getClass().getName() + ":insertFoundChange:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Pingpp.apiKey = Constants.Live_Secret_Key;
            Map<String, Object> chargeParams = new HashMap<String, Object>();
            chargeParams.put("order_no", CommonUtil.getOrderNub());
            chargeParams.put("amount", mfchg_change_amount * 100);  //单位为分
//            chargeParams.put("amount", 1);  //单位为分，仅在测试的时候使用，否则请注释掉
            Map<String, String> app = new HashMap<String, String>();
            app.put("id", Constants.APP_ID);
            chargeParams.put("app", app);
            chargeParams.put("channel", channel);  //微信wx
            chargeParams.put("currency", "cny");
            chargeParams.put("client_ip", ip);
            chargeParams.put("subject", "外教君充值");
            chargeParams.put("body", "外教君充值");
            Charge charge = Charge.create(chargeParams);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("跳转成功");
            Map<String, Object> map = new HashMap<>();
            map.put("charge", charge);
            retInfo.setObj(map);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(), e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
